﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.ZhaoLuService;

namespace CommunitySystem
{
    public partial class ZhaoLuJiHuaControl : UserControl
    {
        private ZhaoLuServiceClient Client = new ZhaoLuServiceClient();
        public ZhaoLuJiHuaControl()
        {
            InitializeComponent();
            for (int i = DateTime.Today.Year + 1; i > DateTime.Today.Year - 10; i--)
            {
                ComboBoxItem cbi = new ComboBoxItem();
                cbi.Content = i.ToString();
                cbi.Tag = i;
                nian.Items.Add(cbi);
            }
            nian.SelectedIndex = 1;
            Load();
        }

        public void Load()
        {
            Client.GetZhaoLuJiHuaModelCompleted -= new EventHandler<GetZhaoLuJiHuaModelCompletedEventArgs>(Client_GetZhaoLuJiHuaModelCompleted);
            Client.GetZhaoLuJiHuaModelCompleted += new EventHandler<GetZhaoLuJiHuaModelCompletedEventArgs>(Client_GetZhaoLuJiHuaModelCompleted);
            Client.GetZhaoLuJiHuaModelAsync(0, islock, DateTime.Today.Year + 1 - nian.SelectedIndex);
        }

        void Client_GetZhaoLuJiHuaModelCompleted(object sender, GetZhaoLuJiHuaModelCompletedEventArgs e)
        {
            DGRole.ItemsSource = e.Result;
        }

        private string islock = "0";
        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            islock = "1";
            EditData.IsEnabled = false;
        }

        private void chkHiddenCols_Unchecked(object sender, RoutedEventArgs e)
        {
            islock = "0";
            EditData.IsEnabled = true;
        }
        /// <summary>
        /// 新增
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void NewData_Click(object sender, RoutedEventArgs e)
        {
            //if (orgids.Count > 1)
            //{
            //    SilverlightMessageBoxLibrary.Message.ErrorMessage("只能针对一个单位进行职位信息的新增！");
            //    return;
            //}
            ZhaoLuJiHuaTitleWindow RMWindow = new ZhaoLuJiHuaTitleWindow(DateTime.Today.Year + 1 - nian.SelectedIndex, 0, true);
            RMWindow.Closed += new EventHandler(RMWindow_Closed);
            RMWindow.Show();
        }
        void RMWindow_Closed(object sender, EventArgs e)
        {
            Load();
        }
        /// <summary>
        /// 详细内容
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as ZhaoLuJiHuaModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                ZhaoLuJiHuaWindow RMWindow = new ZhaoLuJiHuaWindow(seljw.ID, seljw.ID, DateTime.Today.Year + 1 - nian.SelectedIndex, seljw.Name, false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的计划！");
        }
        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void EditData_Click1(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as ZhaoLuJiHuaModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                ZhaoLuJiHuaTitleWindow RMWindow = new ZhaoLuJiHuaTitleWindow(DateTime.Today.Year + 1 - nian.SelectedIndex, seljw.ID, false);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要编辑的计划！");
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void DeleteData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as ZhaoLuJiHuaModel;
            if (seljw != null)
            {
                if (MessageBox.Show("确定删除吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.DeleteZhaoLuJiHuaCompleted -= new EventHandler<DeleteZhaoLuJiHuaCompletedEventArgs>(Client_DeleteZhaoLuJiHuaCompleted);
                    Client.DeleteZhaoLuJiHuaCompleted += new EventHandler<DeleteZhaoLuJiHuaCompletedEventArgs>(Client_DeleteZhaoLuJiHuaCompleted);
                    Client.DeleteZhaoLuJiHuaAsync(seljw.ID);
                }
                else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的计划！");
            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要删除的计划！");
        }

        void Client_DeleteZhaoLuJiHuaCompleted(object sender, DeleteZhaoLuJiHuaCompletedEventArgs e)
        {
            Load();
        }
        /// <summary>
        /// 查看
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ViewData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as ZhaoLuJiHuaModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {
                ZhaoLuJiHuaWindow RMWindow = new ZhaoLuJiHuaWindow(seljw.ID, seljw.ID, DateTime.Today.Year + 1 - nian.SelectedIndex, seljw.Name, true);
                RMWindow.Closed += new EventHandler(RMWindow_Closed);
                RMWindow.Show();

            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要查看的计划！");
        }

        private void ChaData_Click(object sender, RoutedEventArgs e)
        {
            Load();
        }

        private void dgXQ_LoadingRow(object sender, DataGridRowEventArgs e)
        {
        }

        private void DaoChuData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as ZhaoLuJiHuaModel;

            //throw new NotImplementedException();
            if (seljw != null)
            {


            }
            else SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要查看的计划！");
        }
        /// <summary>
        /// 锁定本期工作
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSuoData_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("确定锁定本期工作吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
            {
                Client.SuoZhaoLuZhiWeiCompleted -= new EventHandler<SuoZhaoLuZhiWeiCompletedEventArgs>(Client1_SuoZhaoLuZhiWeiCompleted);
                Client.SuoZhaoLuZhiWeiCompleted += new EventHandler<SuoZhaoLuZhiWeiCompletedEventArgs>(Client1_SuoZhaoLuZhiWeiCompleted);
                Client.SuoZhaoLuZhiWeiAsync();
            }
        }
        /// <summary>
        /// 解锁
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnJieSuoData_Click(object sender, RoutedEventArgs e)
        {
            var seljw = this.DGRole.SelectedItem as ZhaoLuJiHuaModel;
            if(seljw != null)
            {
                if (MessageBox.Show("确定解锁本期工作吗？", "提示", MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                    Client.JieSuoZhaoLuZhiWeiCompleted -= new EventHandler<JieSuoZhaoLuZhiWeiCompletedEventArgs>(Client_JieSuoZhaoLuZhiWeiCompleted);
                    Client.JieSuoZhaoLuZhiWeiCompleted += new EventHandler<JieSuoZhaoLuZhiWeiCompletedEventArgs>(Client_JieSuoZhaoLuZhiWeiCompleted);
                    Client.JieSuoZhaoLuZhiWeiAsync(seljw.ID);
                }
            }
            else
            {
                SilverlightMessageBoxLibrary.Message.ErrorMessage("请选择要解锁的计划！");
            }
        }

        void Client_JieSuoZhaoLuZhiWeiCompleted(object sender, JieSuoZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("解锁成功！");
            Load();
        }

        void Client1_SuoZhaoLuZhiWeiCompleted(object sender, SuoZhaoLuZhiWeiCompletedEventArgs e)
        {
            SilverlightMessageBoxLibrary.Message.InfoMessage("锁定成功！");
            Load();
        }
    }
}
